package com.maple.utils.util

import android.content.Context
import android.os.Build
import com.hjq.permissions.OnPermissionCallback
import com.hjq.permissions.Permission
import com.hjq.permissions.XXPermissions

class PermissionUtils {
    companion object {

        fun requestPermissions(context: Context,result: RequestPermissionResult) {
            val permissionList: MutableList<String> = mutableListOf()
            // 读写
//            if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.TIRAMISU) {
//                permissionList.add(Permission.READ_MEDIA_IMAGES)
//                permissionList.add(Permission.READ_MEDIA_IMAGES)
//                permissionList.add(Permission.READ_MEDIA_IMAGES)
//            } else {
//                permissionList.add(Permission.WRITE_EXTERNAL_STORAGE)
//                permissionList.add(Permission.READ_EXTERNAL_STORAGE)
//            }
//            if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.R) {
//                permissionList.add(Permission.MANAGE_EXTERNAL_STORAGE)
//            }

//            //相机
//            permissionList.add(Permission.CAMERA)
//            //手机状态
//            permissionList.add(Permission.READ_PHONE_STATE)
//            // 蓝牙
//            permissionList.add(Permission.BLUETOOTH_SCAN)
//            permissionList.add(Permission.BLUETOOTH_CONNECT)
//            permissionList.add(Permission.BLUETOOTH_ADVERTISE)
//            //根据实际需要申请定位权限
//            permissionList.add(Permission.ACCESS_COARSE_LOCATION)
//            permissionList.add(Permission.ACCESS_FINE_LOCATION)
//            // 如果不需要在后台使用定位功能，请不要申请此权限
//            permissionList.add(Permission.ACCESS_BACKGROUND_LOCATION)

            XXPermissions.with(context)
                .permission(permissionList)
                .request(object : OnPermissionCallback {

                    override fun onGranted(permissions: MutableList<String>, allGranted: Boolean) {
                        if (allGranted) {
                            result.onRequestPermissionSuccess()
                        } else {
                            result.onRequestPermissionFailure(permissions)
                        }
                    }

                    override fun onDenied(permissions: MutableList<String>, doNotAskAgain: Boolean) {
                        if (doNotAskAgain) {
                            // 如果是被永久拒绝就跳转到应用权限系统设置页面
//                            XXPermissions.startPermissionActivity(context, permissions)
                            result.onRequestPermissionFailureWithAskNeverAgain(permissions)
                        } else {
                            result.onRequestPermissionFailure(permissions)
                        }
                    }
                })
        }

        fun requestLocation(context: Context,result: RequestPermissionResult) {
            val permissionList: MutableList<String> = mutableListOf()
            permissionList.add(Permission.ACCESS_COARSE_LOCATION)
            permissionList.add(Permission.ACCESS_FINE_LOCATION)
             // 如果不需要在后台使用定位功能，请不要申请此权限
            permissionList.add(Permission.ACCESS_BACKGROUND_LOCATION)
            XXPermissions.with(context)
                .permission(permissionList)
                .request(object : OnPermissionCallback {

                    override fun onGranted(permissions: MutableList<String>, allGranted: Boolean) {
                        if (allGranted) {
                            result.onRequestPermissionSuccess()
                        } else {
                            result.onRequestPermissionFailure(permissions)
                        }
                    }

                    override fun onDenied(permissions: MutableList<String>, doNotAskAgain: Boolean) {
                        if (doNotAskAgain) {
                            // 如果是被永久拒绝就跳转到应用权限系统设置页面
//                            XXPermissions.startPermissionActivity(context, permissions)
                            result.onRequestPermissionFailureWithAskNeverAgain(permissions)
                        } else {
                            result.onRequestPermissionFailure(permissions)
                        }
                    }
                })
        }

        fun applyPermissionsByAio(context: Context,result: RequestPermissionResult) {
            val permissionList: MutableList<String> = mutableListOf()
            // 读写
            permissionList.add(Permission.MANAGE_EXTERNAL_STORAGE)
            /// 蓝牙
            permissionList.add(Permission.BLUETOOTH_SCAN)
            permissionList.add(Permission.BLUETOOTH_CONNECT)
            permissionList.add(Permission.BLUETOOTH_ADVERTISE)
            //根据实际需要申请定位权限
            permissionList.add(Permission.ACCESS_FINE_LOCATION)
            permissionList.add(Permission.ACCESS_COARSE_LOCATION)
            permissionList.add(Permission.MANAGE_EXTERNAL_STORAGE)
            //相机
            permissionList.add(Permission.CAMERA)
            //手机状态
            permissionList.add(Permission.READ_PHONE_STATE)

            XXPermissions.with(context)
                .permission(permissionList)
                .request(object : OnPermissionCallback {

                    override fun onGranted(permissions: MutableList<String>, allGranted: Boolean) {
                        if (allGranted) {
                            result.onRequestPermissionSuccess()
                        } else {
                            result.onRequestPermissionFailure(permissions)
                        }
                    }

                    override fun onDenied(permissions: MutableList<String>, doNotAskAgain: Boolean) {
                        if (doNotAskAgain) {
                            // 如果是被永久拒绝就跳转到应用权限系统设置页面
//                            XXPermissions.startPermissionActivity(context, permissions)
                            result.onRequestPermissionFailureWithAskNeverAgain(permissions)
                        } else {
                            result.onRequestPermissionFailure(permissions)
                        }
                    }
                })
        }
        fun requestCamera2(context: Context,result: RequestPermissionResult) {
            val permissionList: MutableList<String> = mutableListOf()
            // 读写
            if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.TIRAMISU) {
                permissionList.add(Permission.READ_MEDIA_IMAGES)
                permissionList.add(Permission.READ_MEDIA_IMAGES)
                permissionList.add(Permission.READ_MEDIA_IMAGES)
                permissionList.add(Permission.WRITE_EXTERNAL_STORAGE)
            } else {
                permissionList.add(Permission.WRITE_EXTERNAL_STORAGE)
                permissionList.add(Permission.READ_EXTERNAL_STORAGE)
            }

            permissionList.add(Permission.MANAGE_EXTERNAL_STORAGE)
            //相机
            permissionList.add(Permission.CAMERA)
            //手机状态
            permissionList.add(Permission.READ_PHONE_STATE)

            XXPermissions.with(context)
                .permission(permissionList)
                .request(object : OnPermissionCallback {

                    override fun onGranted(permissions: MutableList<String>, allGranted: Boolean) {
                        if (allGranted) {
                            result.onRequestPermissionSuccess()
                        } else {
                            result.onRequestPermissionFailure(permissions)
                        }
                    }

                    override fun onDenied(permissions: MutableList<String>, doNotAskAgain: Boolean) {
                        if (doNotAskAgain) {
                            // 如果是被永久拒绝就跳转到应用权限系统设置页面
//                            XXPermissions.startPermissionActivity(context, permissions)
                            result.onRequestPermissionFailureWithAskNeverAgain(permissions)
                        } else {
                            result.onRequestPermissionFailure(permissions)
                        }
                    }
                })
        }

        fun requestCamera(context: Context,result: RequestPermissionResult) {
            val permissionList: MutableList<String> = mutableListOf()
            //相机
            permissionList.add(Permission.CAMERA)
            XXPermissions.with(context)
                .permission(permissionList)
                .request(object : OnPermissionCallback {

                    override fun onGranted(permissions: MutableList<String>, allGranted: Boolean) {
                        if (allGranted) {
                            result.onRequestPermissionSuccess()
                        } else {
                            result.onRequestPermissionFailure(permissions)
                        }
                    }

                    override fun onDenied(permissions: MutableList<String>, doNotAskAgain: Boolean) {
                        if (doNotAskAgain) {
                            // 如果是被永久拒绝就跳转到应用权限系统设置页面
//                            XXPermissions.startPermissionActivity(context, permissions)
                            result.onRequestPermissionFailureWithAskNeverAgain(permissions)
                        } else {
                            result.onRequestPermissionFailure(permissions)
                        }
                    }
                })
        }
        fun requestStorage(context: Context,result: RequestPermissionResult) {
            XXPermissions.with(context)
                .permission(Permission.MANAGE_EXTERNAL_STORAGE)
                .request(object : OnPermissionCallback {

                    override fun onGranted(permissions: MutableList<String>, allGranted: Boolean) {
                        if (allGranted) {
                            result.onRequestPermissionSuccess()
                        } else {
                            result.onRequestPermissionFailure(permissions)
                        }
                    }

                    override fun onDenied(permissions: MutableList<String>, doNotAskAgain: Boolean) {
                        if (doNotAskAgain) {
                            // 如果是被永久拒绝就跳转到应用权限系统设置页面
//                            XXPermissions.startPermissionActivity(context, permissions)
                            result.onRequestPermissionFailureWithAskNeverAgain(permissions)
                        } else {
                            result.onRequestPermissionFailure(permissions)
                        }
                    }
                })
        }
        fun requestBluetooth(context: Context,result: RequestPermissionResult) {
           val permissionList: MutableList<String> = mutableListOf()
            permissionList.add(Permission.BLUETOOTH_SCAN)
            permissionList.add(Permission.BLUETOOTH_CONNECT)
            permissionList.add(Permission.BLUETOOTH_ADVERTISE)
//            if(Build.VERSION.SDK_INT < Build.VERSION_CODES.S_V2) {
//                //根据实际需要申请定位权限
//                permissionList.add(Permission.ACCESS_FINE_LOCATION)
//                permissionList.add(Permission.ACCESS_COARSE_LOCATION)
//            }

            //根据实际需要申请定位权限
            permissionList.add(Permission.ACCESS_FINE_LOCATION)
            permissionList.add(Permission.ACCESS_COARSE_LOCATION)

            XXPermissions.with(context)
                .permission(permissionList)
                .request(object : OnPermissionCallback {

                    override fun onGranted(permissions: MutableList<String>, allGranted: Boolean) {
                        if (allGranted) {
                            result.onRequestPermissionSuccess()
                        } else {
                            result.onRequestPermissionFailure(permissions)
                        }
                    }

                    override fun onDenied(permissions: MutableList<String>, doNotAskAgain: Boolean) {
                        if (doNotAskAgain) {
                            // 如果是被永久拒绝就跳转到应用权限系统设置页面
//                            XXPermissions.startPermissionActivity(context, permissions)
                            result.onRequestPermissionFailureWithAskNeverAgain(permissions)
                        } else {
                            result.onRequestPermissionFailure(permissions)
                        }
                    }
                })
        }
    }
}


